import config from "@/config"; // 导入配置文件
export const BASE_URL = config.baseURL;
let token12 = ''
export const http = (options) => {
  uni.showLoading({
    title: '加载中'
  })
  uni.getStorage({
    key: 'token',
    success: function(res) {
      token12 = res.data || ''
    }
  })
  return new Promise((resolve, reject) => {
    let header = {
      'content-type': 'application/json',
      'token': token12
    }
    // 　　　　header[tokenName]=token;
    uni.request({
      url: BASE_URL + options.url, //请求地址
      method: options.method || 'GET', //请求方法
      header: options.header || header,
      data: options.data || {}, //参数
      success: (res) => { //成功回调
        if (res.data === 'token illegal') {
          uni.showToast({
            title: '登录过期，请重新登录',
            icon: 'error',
            success: function() {
              uni.navigateTo({
                url: '/pages/login/login',
              })
            }
          })

        }
        if (res.data.code !== 200) {
          reject(res)
          return
        }
        resolve(res);
      },
      fail: (err) => { //失败回调
        uni.showToast({
          icon: 'none',
          title: '请求失败!'
        })
        reject(err)
      },
      complete() {
        uni.hideLoading()
      }
    })
  })
}
